<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input  v-model="searchObj.hosname" placeholder="医院名称"/>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchObj.hoscode" placeholder="医院编号"/>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <div>
      <el-button type="danger" size="mini" @click="batchRemoveHospSet()">批量删除</el-button>
    </div>

    <!-- banner列表 -->
    <el-table
      :data="list"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"/>
      <el-table-column type="index" width="50" label="序号"/>
      <el-table-column prop="hosname" label="医院名称"/>
      <el-table-column prop="hoscode" label="医院编号"/>
      <el-table-column prop="apiUrl" label="api基础路径"width="200"/>
      <el-table-column prop="contactsName" label="联系人姓名"/>
      <el-table-column prop="contactsPhone" label="联系人手机"/>
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? '可用' : '不可用' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="280" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="editHospSet(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row)"></el-button>
          <el-button type="warning" size="mini" v-if="scope.row.status==1" @click="lockHospSet(scope.row.id,0)">锁定</el-button>
          <el-button type="primary" size="mini" v-if="scope.row.status==0" @click="lockHospSet(scope.row.id,1)">取消锁定</el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="currentChange"/>

  </div>

</template>

<script>
import hospSet from "@/api/hospSet";
export default {
  name: "List",
  data(){
    return {
      current: 1,
      limit: 3,
      searchObj: {},
      list: [],
      total: null,
      multipleSelection:[]
    }
  },
  methods:{
    editHospSet(data){
      this.$router.push('/hospSet/edit/'+data.id);
    },
    lockHospSet(id,status){
      hospSet.lockHospSet(id,status).then(resp =>{
         this.getList();
      })
    },
    resetData(){
      this.searchObj = {};
      this.getList();
    },
    handleSelectionChange(selection){
      console.log(selection);
      this.multipleSelection = selection;
    },
    batchRemoveHospSet(){
      this.$confirm(`此操作将永久删除选中的医院设置, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = [];
        this.multipleSelection.forEach(item =>{
          ids.push(item.id);
        });
        hospSet.batchHospSet(ids).then(resp =>{
          this.$message({
            type: 'success',
            message: '批量删除成功!'
          });
          this.getList();
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    removeDataById(data){
      this.$confirm(`此操作将永久删除${data.hosname}医院设置, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        hospSet.deleteHospSet(data.id).then(resp =>{
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    currentChange(currentPage){
      this.current = currentPage;
      this.getList(currentPage);
    },
    getList(page=1){
      this.current = page;
      hospSet.getHospSetList(this.current, this.limit, this.searchObj)
        .then(resp =>{
          this.list = resp.data.records;
          this.total = resp.data.total;
        })
    }
  },
  created() {
    this.getList();
  }

}
</script>

<style scoped>

</style>
